﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Runtime.Remoting.Messaging;
using MessageCallAspect.Inspector;
using MessageCallAspect.Inspector.Request;
using MessageCallAspect.Inspector.MessageSink;
using System.Diagnostics;
using MessageCallAspect.Aspect.Log;
using Syk.Library.Log;

namespace MessageCallAspect.Aspect.Log
{
    [DebuggerStepThrough]
    public class LogMessageSink : SimpleMessageInspector
    {
        private readonly string m_Category;
        #region ctor

        public LogMessageSink(IServiceProvider context, object target, IMessageSink nextSink)
            : this(context, target, null, nextSink)
        { }
        public LogMessageSink(IServiceProvider context, object target, string category, IMessageSink nextSink)
            : base(context, target, nextSink)
        {
            m_Category = category;
        }

        #endregion

        protected override Exception OnException(IMessageRequest request, Exception ex)
        {
            string category = m_Category == null ? request.Target.GetType().Name : m_Category;
            Logger.Instance.Error(category, "Error: {0}.{1}{2}{3}", request.Target.GetType().Name, request.Method.Name, Environment.NewLine,
              ex.ToString());

            return base.OnException(request, ex);
        }
    }
}
